package com.hdoit.ares.logical.team.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.team.dao.ICooperationTeamDao;
import com.hdoit.ares.logical.team.model.CooperationTeam;
import com.hdoit.ares.logical.team.service.ICooperationTeamService;
import com.hdoit.ares.logical.viewmodel.team.CooperationTeamSearch;
import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.logical.superbase.model.RoleEmployee;
import com.tennetcn.common.logical.superbase.service.IEmployeeService;
import com.tennetcn.common.logical.superbase.service.IRoleEmployeeService;
import com.tennetcn.common.logical.superbase.service.IRoleService;
import com.tennetcn.common.message.DaoBaseRuntimeException;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
/**
 * @author      yujinhui
 * @email       
 * @createtime  2018年01月18日 14:27:30
 * @comment
 */
@Component
public class CooperationTeamServiceImpl extends SuperService<CooperationTeam> implements ICooperationTeamService{
	private ICooperationTeamDao cooperationTeamDao;
	public ICooperationTeamDao getCooperationTeamDao() {
		return cooperationTeamDao;
	}
	@Resource
	public void setCooperationTeamDao(ICooperationTeamDao cooperationTeamDao) {
		this.cooperationTeamDao = cooperationTeamDao;
	}

	private IEmployeeService employeeService;

	public IEmployeeService getEmployeeService() {
		return employeeService;
	}

	@Resource
	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	private IRoleService roleService;

	public IRoleService getRoleService() {
		return roleService;
	}

	@Resource
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	
	private IRoleEmployeeService roleEmployeeService;

	public IRoleEmployeeService getRoleEmployeeService() {
		return roleEmployeeService;
	}

	@Resource
	public void setRoleEmployeeService(IRoleEmployeeService roleEmployeeService) {
		this.roleEmployeeService = roleEmployeeService;
	}
	
	@Override
	public List<CooperationTeam> queryList(CooperationTeamSearch cooperationTeamSearch,
			PagerModel pagerModel, String employeeAccount) {
		
		return cooperationTeamDao.queryList(cooperationTeamSearch,pagerModel,employeeAccount);
	}

	@Override
	public boolean applyChange(CooperationTeam cooperationTeam) {
/*		List<Role> roles=roleService.queryListByMark(RoleMapper.personnel);
		List<String> roleIdList=Enumerable.init(roles).map(role->role.getRoleId()).toList();
		personnel.setRoleIds(StringHelper.join(roleIdList));*/
		if(employeeService.applyEmployeeLogin(cooperationTeam)){
			if(!super.applyChange(cooperationTeam)){
				throw new DaoBaseRuntimeException("employeeUser applyChange roll back");
			}
			return saveRolePersonnel(cooperationTeam);
		}
		return false;
	}

	private boolean saveRolePersonnel(CooperationTeam cooperationTeam) {
		if (cooperationTeam.getRoleIds() == null) {
			return true;
		}
		roleEmployeeService.deleteByEmployeeId(cooperationTeam.getEmployeeId());
		return saveRolePersonnel(cooperationTeam.getRoleIds(), cooperationTeam);
	}

	private boolean saveRolePersonnel(String roleIds, CooperationTeam cooperationTeam) {
		if (StringUtils.isEmpty(roleIds)) {
			return true;
		}
		String[] roleIdStrs = roleIds.split(",");
		List<RoleEmployee> roleEmployees = new ArrayList<RoleEmployee>();
		RoleEmployee roleEmployee = null;
		for (String roleId : roleIdStrs) {
			roleEmployee = new RoleEmployee();
			roleEmployee.setId(CommonUtils.getKeyId());
			roleEmployee.setRoleId(roleId);
			roleEmployee.setEmployeeId(cooperationTeam.getEmployeeId());
			roleEmployee.setModelStatus(ModelStatus.add);
			roleEmployee.setDeleteMark(YesOrNoInteger.no);
			if (ModelStatus.add.equals(cooperationTeam.getModelStatus())) {
				roleEmployee.setCreateDate(cooperationTeam.getCreateDate());
				roleEmployee.setCreateUserId(cooperationTeam.getCreateUserId());
				roleEmployee.setCreateUserName(cooperationTeam.getCreateUserName());
			} else if (ModelStatus.update.equals(cooperationTeam.getModelStatus())) {
				roleEmployee.setCreateDate(cooperationTeam.getModifyDate());
				roleEmployee.setCreateUserId(cooperationTeam.getModifyUserId());
				roleEmployee.setCreateUserName(cooperationTeam.getModifyUserName());
			}
			roleEmployees.add(roleEmployee);
		}
		return roleEmployeeService.insertListEx(roleEmployees) == roleEmployees.size();
	}



}